package com.cw.jeeyt.service.impl.system;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.common.utils.StringUtils;
import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.service.bo.system.SystemAttachRelationBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.system.SystemAttachRelationDao;
import com.cw.jeeyt.dao.model.system.SystemAttachRelationDo;
import com.cw.jeeyt.service.converter.system.SystemAttachRelationConverter;
import com.cw.jeeyt.service.intf.system.SystemAttachRelationService;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.common.utils.SequenceTool;

/**
 * 附件关联关系业务实现类
 *
 * @author chenw
 * @version 1.0.0
 */
@Service
public class SystemAttachRelationServiceImpl implements SystemAttachRelationService {

    private static final Logger logger = LoggerFactory.getLogger(SystemAttachRelationServiceImpl.class);
    @Autowired
    private SystemAttachRelationDao systemAttachRelationDao;

    /**
     * 分页查询附件关联关系
     *
     * @param systemAttachRelationBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<SystemAttachRelationBo> queryByPage(SystemAttachRelationBo systemAttachRelationBo, Integer startNum,
        Integer pageSize) throws Exception {
        PageInfo<SystemAttachRelationDo> pageInfo = systemAttachRelationDao.queryByPage(
            SystemAttachRelationConverter.bo2Do(systemAttachRelationBo), startNum, pageSize);
        return new PageInfo<>(pageInfo, SystemAttachRelationConverter.doList2BoList(pageInfo.getList()));
    }

    /**
     * 根据主键查寻附件关联关系信息
     *
     * @param id
     * @return
     */
    @Override
    public SystemAttachRelationBo getSystemAttachRelationById(String id) throws Exception {

        if (StringUtils.isEmpty(id)) {
            logger.error("--主键id为空--");
            return null;
        }

        return SystemAttachRelationConverter.do2Bo(systemAttachRelationDao.getById(id));
    }

    /**
     * 新增附件关联关系
     *
     * @param systemAttachRelationBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insert(SystemAttachRelationBo systemAttachRelationBo) throws Exception {

        if (systemAttachRelationBo == null) {
            logger.error("附件关联关系为空");
            return null;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        SystemAttachRelationDo systemAttachRelation = SystemAttachRelationConverter.bo2Do(systemAttachRelationBo);
        systemAttachRelation.setId(SequenceTool.nextId());
        systemAttachRelation.setCreateBy(userVo.getId());
        systemAttachRelation.setCreateDate(new Date());
        systemAttachRelation.setUpdateBy(userVo.getId());
        systemAttachRelation.setUpdateDate(new Date());
        return systemAttachRelationDao.insert(systemAttachRelation);
    }

    /**
     * 更新附件关联关系信息
     *
     * @param systemAttachRelationBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(SystemAttachRelationBo systemAttachRelationBo) throws Exception {

        if (systemAttachRelationBo == null) {
            logger.error("附件关联关系为空");
            return;
        }

        SystemAttachRelationDo systemAttachRelation = SystemAttachRelationConverter.bo2Do(systemAttachRelationBo);
        systemAttachRelation.setUpdateBy(SessionUtil.getSessionUserId());
        systemAttachRelation.setUpdateDate(new Date());

        systemAttachRelationDao.update(systemAttachRelation);
    }

    /**
     * 删除附件关联关系
     *
     * @param systemAttachRelationBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(SystemAttachRelationBo systemAttachRelationBo) throws Exception {

        if (systemAttachRelationBo == null) {
            logger.error("附件关联关系为空");
            return;
        }
        systemAttachRelationDao.delete(SystemAttachRelationConverter.bo2Do(systemAttachRelationBo));
    }

    @Override
    public void deleteByObjectIdAndAttachId(String objectId, String attachId) throws Exception {
        if (StringUtils.isEmpty(objectId)) {
            logger.error("objectId为空");
            return;
        }
        if (StringUtils.isEmpty(attachId)) {
            logger.error("attachId为空");
            return;
        }
        SystemAttachRelationDo systemAttachRelation = new SystemAttachRelationDo();
        systemAttachRelation.setAttachId(attachId);
        systemAttachRelation.setObjectId(objectId);
        systemAttachRelationDao.deleteByObjectIdAndAttachId(systemAttachRelation);
    }
}
